// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
mod download_manager;
use download_manager::{create_download_task, start_download, pause_download, resume_download, delete_download};
mod my_http;
mod my_ftp;
mod my_torrent;
mod init;
use init::{initialize_session, update_session_data, get_config, update_config, initialize_config, get_session_data};
mod window_title;
use window_title::set_window_title;
mod window_events;
use window_events::{close_window, handle_window_close_requested};

#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    // 初始化配置
    if let Err(e) = initialize_config() {
        eprintln!("Failed to initialize config: {}", e);
    }
    if let Err(e) = initialize_session() {
        eprintln!("Failed to initialize session: {}", e);
    }

    let builder = tauri::Builder::default()
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_clipboard_manager::init())
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_opener::init())
        .setup(|app| {
            handle_window_close_requested(app.handle().clone());
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![
            greet,
            create_download_task,
            start_download,
            pause_download,
            resume_download,
            delete_download,
            update_session_data,
            get_config,
            update_config,
            get_session_data,
            set_window_title,
            close_window,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
